// pages/order/queue.js
var common = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    queue: null,
    catid: null
  },
  goQueue: function() {
    var that = this;
    var item = {
      categoryId: that.data.catid,
      nickName: that.data.userInfo.nickName,
      guest_id: app.globalData.openid
    };
    wx.request({
      url: common.baseUrl + '/queue/upsert',
      method: "POST",
      data: item,
      success: function(res) {
        if (res.code == 0) {
          that.showSuccess();
        }
        else {
          var errText = res.data.codemsg;
          wx.showModal({
            title: '已排队',
            content: '已经排过队了，主子是否立即查看',
            success: function() {
                wx.switchTab({
                  url: '../order/my',
                })
            }
          })
          // that.showSuccess();
        }
      }
    })
  },
  goBack: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  showSuccess() {
    wx.showToast({
      title: '排队成功!',
      icon: 'success',
      duration: 4000,
      success: function() {
        wx.switchTab({
          url: '../order/my',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options['catid'])
    that.setData({
      catid: options['catid']
    })
    app.getUserInfo(function(userInfo){
      console.log(userInfo);
      that.setData({
        userInfo: userInfo
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.reLoad();
  },

  reLoad: function () {
    var id = this.data.catid;
    console.log(id);
    var that = this;
    wx.request({
      url: common.baseUrl + '/category/queues',
      method: "POST",
      data: {
        _id: id
      },
      success: function(res) {
        that.setData({
          queue: res.data.result[0]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})